package cn.dkdoo.utils.templateCommon.mongo;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.List;

/**
 * @author nicky
 * @date 2021/7/5 上午11:10
 */
public class DkdCriteria extends Criteria {

    public void addFilterShop(Integer belongType, List<String> shopIds, String shopId) {
        DkdMongoUtils.addFilterShop(this, belongType, shopIds, shopId);
    }

    public void addFilterShop(Integer belongType, String companyId, String shopId) {
        DkdMongoUtils.addFilterShop(this, belongType, companyId, shopId);
    }

    public void addRange(String fieldName, Object startTime, Object endTime) {
        DkdMongoUtils.addRange(this, fieldName, startTime, endTime);
    }

    /**
     * 范围查找 右区间为闭区间->gte
     */
    public void addRangeLeft(String fieldName, Object startTime, Object endTime) {
        DkdMongoUtils.addRangeLeft(this, fieldName, startTime, endTime);
    }

    /**
     * 范围查找 左区间为闭区间->lte
     */
    public void addRangeRight(String fieldName, Object startTime, Object endTime) {
        DkdMongoUtils.addRangeRight(this, fieldName, startTime, endTime);
    }

    public void addRanges(String startFieldName, Object startTime, String endFieldName, Object endTime) {
        DkdMongoUtils.addRanges(this, startFieldName, startTime, endFieldName, endTime);
    }

    public void orOperator(List<DkdCriteria> criterias) {
        super.orOperator(criterias.toArray(new Criteria[criterias.size()]));
    }

    public void norOperator(List<DkdCriteria> criterias) {
        super.norOperator(criterias.toArray(new Criteria[criterias.size()]));
    }

    public void andOperator(List<DkdCriteria> criterias) {
        super.andOperator(criterias.toArray(new Criteria[criterias.size()]));
    }

    public void isNotNull(String key, Object o) {
        if (o == null) {
            return;
        }
        super.and(key).is(o);
    }

    public void isNotNull(String key, String o) {
        if (StringUtils.isEmpty(o)) {
            return;
        }
        super.and(key).is(o);
    }

    public void inNotNull(String key, Object... o) {
        if (o == null || o.length == 0) {
            return;
        }
        super.and(key).in(o);
    }

    public DkdCriteria inNotNull(String key, Collection<?> c) {
        if (CollectionUtils.isEmpty(c)) {
            return this;
        }
        super.and(key).in(c);
        return this;
    }
}
